/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lose.ucm;

import java.sql.Blob;
import database.Select;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import javax.swing.ImageIcon;
import lose.model.ValueModel;
import lose.view.LoseReport;

/**
 *
 * @author Donghui
 */
public class VerifyCheck {

    private static VerifyCheck local;
    private String tppath="D:\\TP_BUFFER\\";
    private String path;
    private String tp;
//    private String[] spliter;
    private String id;
    private String psw;
    private String sql;
    private ResultSet result;
    private File file;
    private byte[] buffer=new byte[4096];
    private ValueModel value;

    public static VerifyCheck getInstance()
    {
        if(local==null)
        {
            local=new VerifyCheck();
            local.value=LoseReport.getInstance().getValueModel();
        }
        else
        {
            local.value=LoseReport.getInstance().getValueModel();
        }
        return local;
    }

    public void setPhoto(){
        id=value.getPid();
        psw=value.getPsw();
        try {
            InputStream inputimage=null;
            OutputStream outputimage=null;
            sql="SELECT roll_id FROM rfid WHERE roll_id='"+ id +"' AND psw='"+ psw +"'";
            result=Select.getInstance().selectValue(sql);
            result.next();
                tp=result.getString("roll_id");

            sql="SELECT photo FROM roll WHERE id='"+ tp +"'";
            result=Select.getInstance().selectValue(sql);
            result.next();
            path=tppath+id+".jpg";
            System.out.println("path: " + path);
            file=new File(path);
            if(!file.exists())
            {
                try{
                 file.createNewFile();
                }
                catch(IOException e)
                {
                e.printStackTrace();
                }
            }
            try{
                outputimage=new FileOutputStream(file);
            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }

                Blob image=result.getBlob("photo");
                inputimage=image.getBinaryStream();
                int size;
                try{
                    while((size=inputimage.read(buffer))!=-1)
                    {
                     outputimage.write(buffer, 0, size);
                    }
                }
                    catch (IOException e) {
                    e.printStackTrace();
                    }

                outputimage.close();
                inputimage.close();
        LoseReport.getInstance().getButtonModel().getNotice().setText("Approved!");
        Image imgTemp=Toolkit.getDefaultToolkit().createImage(path);
        LoseReport.getInstance().getButtonModel().getPhoto().setIcon(new ImageIcon(imgTemp));
        LoseReport.getInstance().getButtonModel().getPhoto().repaint();
        LoseReport.getInstance().getButtonModel().getCid().setEnabled(true);
        LoseReport.getInstance().getButtonModel().getSubmit().setEnabled(true);
        LoseReport.getInstance().getButtonModel().getNewPsw().setEnabled(true);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        LoseReport.getInstance().getButtonModel().getNotice().setText("Denied!");
        Image imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_BUFFER\\error.jpg");
        LoseReport.getInstance().getButtonModel().getPhoto().setIcon(new ImageIcon(imgTemp));
        LoseReport.getInstance().getButtonModel().getCid().setEnabled(false);
        LoseReport.getInstance().getButtonModel().getSubmit().setEnabled(false);
        LoseReport.getInstance().getButtonModel().getNewPsw().setEnabled(false);        
        }
    }
}
